import java.util.Arrays;

/**
 * @ClassName LC238
 * 除自身以外数组的乘积
 * @Author ykx
 * @Date 2024/3/10 11:22
 * @Version 1.0
 */
public class LC238 {

    public static void main(String[] args) {
        int[] nums = new int[]{4, 3, 2, 1};
        LC238 lc238 = new LC238();
        System.out.println(Arrays.toString(lc238.productExceptSelf(nums)));
    }

    public int[] productExceptSelf(int[] nums) {
        int[] result = new int[nums.length];
        int pre = 1, suf = 1;
        for (int i = 0; i < nums.length; i++) {
            result[i] = pre;
            pre *= nums[i];
        }

        for (int i = nums.length - 1; i >= 0; i--) {
            result[i] *= suf;
            suf *= nums[i];
        }
        return result;
    }
}
